package com.quick.driver.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quick.common.Constent.Quick;
import com.quick.common.Exception.exception.BaseException;
import com.quick.common.bean.Driver;
import com.quick.common.bean.User;
import com.quick.common.dto.ImageDTO;
import com.quick.common.dto.PasswordDTO;
import com.quick.common.dto.RegisterDTO;
import com.quick.common.dto.LoginDTO;
import com.quick.common.service.IDriverService;
import com.quick.common.util.PasswordUtil;
import com.quick.common.util.TokenUtil;
import com.quick.driver.mapper.DriverMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {

    @Autowired
    private DriverMapper driverMapper;
    @Override
    public String driverLogin(LoginDTO loginDTO) {
        if (loginDTO.getName() == null || loginDTO.getName().isEmpty()){
            throw new BaseException(400,"司机名不能为空");
        }
        if (loginDTO.getPassword() == null || loginDTO.getPassword().isEmpty()){
            throw new BaseException(400,"密码不能为空");
        }
        Driver driver = super.getOne(new QueryWrapper<Driver>().eq("name", loginDTO.getName()));
        if (driver == null){
            throw new BaseException(400,"司机不存在");
        }
        if(!PasswordUtil.checkPassword(loginDTO.getPassword(), driver.getPassword())){
            throw new BaseException(400,"密码错误");
        }
        return TokenUtil.getToken(driver.getId(), driver.getName(), Quick.TOKEN_KEY_ROLE_DRIVER);
    }

    @Override
    public void driverRegister(RegisterDTO registerDTO) {
        System.out.println(registerDTO);
        if (registerDTO.getName() == null || registerDTO.getName().isEmpty()){
            throw new BaseException(400,"用户名不能为空");
        }
        if (registerDTO.getPassword() == null || registerDTO.getPassword().isEmpty()){
            throw new BaseException(400,"密码不能为空");
        }
        if (registerDTO.getRePassword() == null || registerDTO.getRePassword().isEmpty()){
            throw new BaseException(400,"确认密码不能为空");
        }
        if (!registerDTO.getPassword().equals(registerDTO.getRePassword())){
            throw new BaseException(400,"密码不一致");
        }
        if (super.getOne(new QueryWrapper<Driver>().eq("name", registerDTO.getName())) != null){
            throw new BaseException(400,"用户名已存在");
        }
        Driver driver = new Driver();
        BeanUtils.copyProperties(registerDTO, driver);
        driver.setPassword(PasswordUtil.getEncryption(driver.getPassword()));
        super.save(driver);
    }

    @Override
    public List<Driver> nearByFreeDrivers(Double longitude, Double latitude) {
        System.out.println("cnm");
        return driverMapper.nearByFreeDrivers(longitude, latitude);
    }

    @Override
    public void editStatus(Integer id, Byte status) {
        super.update().set("status",status).eq("id",id).update();
    }

    @Override
    public Driver getDriverById(Integer id) {
        return super.getById(id);
    }


    @Override
    public int updateDriver(Driver driver) {
        return super.updateById(driver) ? 1 : 0;
    }

    @Override
    public int deleteDriver(Integer id) {
        return super.removeById(id) ? 1 : 0;
    }


    @Override
    public List<Driver> getAll(Driver driver) {
        return super.list(new QueryWrapper<>(driver));
    }

    @Override
    public IPage<Driver> getPage(Driver driver, IPage page) {
        LambdaQueryWrapper<Driver> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Driver::getLicense, driver.getLicense())
                .like(Driver::getStatus, driver.getStatus());
        return super.page(page, queryWrapper);
    }

    @Override
    public String upload(ImageDTO imageDTO) {
        Driver driver=new Driver();
        BeanUtils.copyProperties(imageDTO, driver);
        super.updateById(driver);
        Driver driver1 = super.getById(driver.getId());
        return driver1.getImg();
    }

    @Override
    public void changePassword(PasswordDTO passwordDTO) {
        Driver driver = super.getById(passwordDTO.getId());
        if (driver == null){
            throw new BaseException(400,"用户不存在");
        }
        if (!PasswordUtil.checkPassword(passwordDTO.getOldPassword(), driver.getPassword())){
            throw new BaseException(400,"密码错误");
        }
//        if(!PasswordUtil.checkPassword(passwordDTO.getNewPassword(),user.getPassword())){
//            throw new BaseException(400,"新密码不能与旧密码一致");
//        }
        driver.setPassword(PasswordUtil.getEncryption(passwordDTO.getNewPassword()));
        super.updateById(driver);
    }
}
